﻿using LogNaga.Logs;
using System;

namespace LogNaga.Logs
{
	/// <summary>
	/// 一个wrapper,方便使用线程安全的FileLog
	/// </summary>
	public class ThreadSafeFileLog : Log
	{
		private string fileName;
		private ThreadSafeLogDecorator log;

		/// <summary>
		/// 创建一个<see cref="ThreadSafeFileLog"/>实例
		/// </summary>
		/// <param name="fileName">文件名</param>
		public ThreadSafeFileLog(string fileName)
			:this(fileName, ExceptionFormatterHelper.Default)
		{ }

		/// <summary>
		/// 创建一个<see cref="ThreadSafeFileLog"/>实例
		/// </summary>
		/// <param name="fileName">文件名</param>
		public ThreadSafeFileLog(string fileName, ExceptionFormatter exceptionFormatter)
		{
			if (string.IsNullOrEmpty(fileName))
				throw new ArgumentNullException("fileName");

			this.fileName = fileName;
			log = new ThreadSafeLogDecorator(new AutoCutFileLog(fileName, exceptionFormatter));
		}

		/// <summary>
		/// 记录数据
		/// </summary>
		/// <param name="errorEntry"><see cref="Error"/>实例</param>
		public override void Write(Error errorEntry)
		{
			log.Write(errorEntry);
		}
	}
}
